import Dialog from '../../dist/dialog/dialog';
var call = require("../../utils/request.js");
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
var app = getApp();
Page({
  data: {
    active: 0,
    page: 1,
    size: 5,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    circular: true,
    leftMargin: '80rpx',
    rightMargin: '80rpx',
    currentIndex: 0,

    locateCity: '',
    latitude: 0, //纬度
    longitude: 0, //经度
    adv: [
      '../../images/swiper/swiper01.png',
      '../../images/swiper/swiper02.png',
      '../../images/swiper/swiper03.jpg',
      '../../images/swiper/swiper04.jpg',
    ], //广告图片
    shopType: [], //商家类型
    shopList: [], //商家列表
    userInfo: [], //用户信息
  },
  //发送请求 1:广告 2:店铺类型 3:附近商铺 4:全国省市区位置
  onLoad: function(options) {
    console.log('用户信息:' + app.globalData.userInfo);
    console.log('用户id:' + app.globalData.userId);
    console.log('用户openid:' + app.globalData.openId);
    var that = this;
    // call.getData('adv/advLocation?location=weixinhead', {}, this.getAdv, "");
    call.getData('shopType/getAllShopType', {}, this.getShopType, "");
    call.getData('shop/getNearbyShop', {
      page: this.data.page,
      size: this.data.size,
      latitude: this.data.latitude,
      longitude: this.data.longitude
    }, this.getShopList, "");

    // 实例化腾讯地图API核心类
    qqmapsdk = new QQMapWX({
      key: 'AK3BZ-ODVCX-UPV4L-ZJAIY-ASTBQ-O6BPD' // 必填
    });
    //1、获取当前位置坐标
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function(addressRes) {
            var address = addressRes.result.formatted_addresses.recommend;
            that.setData({

            })
            //console.log(address)
          }
        })
      }
    })
  },
  handleChange: function(e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  getAdv: function (data) {
    console.log(data);
    var that = this;
    that.setData({
      adv: data
    })
  },
  getShopType: function(data) {
    var that = this;
    that.setData({
      shopType: data
    })
  },
  getShopList: function(data) {
    console.log(data);
    var that = this;
    that.setData({
      shopList: data
    })
  },
  //跳转到店铺菜单
  onMenu: function(event) {
    console.log(event);
    var id = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    var img = event.currentTarget.dataset.img;
    var address = event.currentTarget.dataset.address;
    var time = event.currentTarget.dataset.time;
    var phone = event.currentTarget.dataset.phone;
    var startTime = event.currentTarget.dataset.starttime;
    var endTime = event.currentTarget.dataset.endtime;
    var lat = event.currentTarget.dataset.latitude;
    var long = event.currentTarget.dataset.longitude;
    wx.navigateTo({
      url: '../menu/menu?id=' + id + '&img=' + img + '&name=' + name + '&address=' + address + '&time=' + time + "&phone=" + phone + "&startTime=" + startTime + "&endTime=" + endTime + "&lat=" + lat + "&long=" + long,
    })
  },
  //跳转到城市
  // onSelectCity: function(event) {
  //   console.log("跳转选择城市");
  //   wx.navigateTo({
  //     url: '../selectCity/selectCity?locateCity=' + this.data.locateCity,
  //   })
  // },
  // 扫码
  scanCode: function(event) {
    console.log("跳转到扫码");
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res);
      }
    })
  },
  onShow: function() {
    let vm = this;
    vm.getUserLocation();
  },
  getUserLocation: function() {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        // console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function(res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function(dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        } else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },
  // 微信获得经纬度
  getLocation: function() {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        // console.log(JSON.stringify(res))
        vm.setData({
          latitude: res.latitude.toString(),
          longitude: res.longitude.toString()
        })
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function(res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  // 获取当前地理位置
  getLocal: function(latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function(res) {
        let locateCity = res.result.address;
        let latitude = res.result.location.lat.toString();
        let longitude = res.result.location.lng.toString();
        vm.setData({
          latitude: latitude,
          longitude: longitude,
          locateCity: locateCity
        })
      },
      fail: function(res) {
        // console.log(res);
      },
      complete: function(res) {
        // console.log(res);
      }
    });
  }
})